package com.youkeyi.ddy.cloud.application.business.crm.source.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.source.BusinessSourceCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.source.dto.BusinessSourceListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.source.dto.BusinessSourceListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.source.param.BusinessSourceCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.source.param.BusinessSourceDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.source.param.BusinessSourceListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.source.param.BusinessSourceUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.CrmSourceCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.dto.CrmSourceListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.param.CrmSourceCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.param.CrmSourceDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.param.CrmSourceQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.source.param.CrmSourceUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessSourceController
 *
 * @Author liuxiawang
 * @Date 2024/4/7 10:44 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessSourceController implements BusinessSourceCloudService {

    @Autowired
    private CrmSourceCloudService crmSourceCloudService;

    @Override
    public BusinessSourceListCloudDto list(BusinessSourceListCloudParam param) {
        BusinessSourceListCloudDto dto = new BusinessSourceListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmSourceQueryCloudParam sourceQueryCloudParam = new CrmSourceQueryCloudParam();
        sourceQueryCloudParam.setSourcePrimaryKeys(param.getSourcePrimaryKeys());
        sourceQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        sourceQueryCloudParam.setCodes(param.getCodes());
        sourceQueryCloudParam.setSourceFuzzy(param.getSourceFuzzy());
        sourceQueryCloudParam.setPageSize(param.getPageSize());
        sourceQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmSourceListCloudDto sourceDtos = crmSourceCloudService.query(sourceQueryCloudParam);
        if (null != sourceDtos && CollectionUtils.isNotEmpty(sourceDtos.getRows())) {
            List<BusinessSourceListItemCloudDto> items = new ArrayList<>();
            sourceDtos.getRows().forEach(source -> {
                BusinessSourceListItemCloudDto item = new BusinessSourceListItemCloudDto();
                item.setSourcePrimaryKey(source.getSourcePrimaryKey());
                item.setCorpKey(source.getCorpKey());
                item.setCode(source.getCode());
                item.setSource(source.getSource());
                item.setSourcePersonName(source.getSourcePersonName());
                item.setSourcePersonPhone(source.getSourcePersonPhone());
                item.setRemark(source.getRemark());
                item.setIsDeleted(source.getIsDeleted());
                item.setIsDeletedName(source.getIsDeletedName());
                item.setCreateUserKey(source.getCreateUserKey());
                item.setCreateUserName(source.getCreateUserName());
                item.setCreateTime(source.getCreateTime());
                item.setUpdateUserKey(source.getUpdateUserKey());
                item.setUpdateUserName(source.getUpdateUserName());
                item.setUpdateTime(source.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(sourceDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessSourceCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getSource())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmSourceCreateCloudParam sourceCreateCloudParam = new CrmSourceCreateCloudParam();
        sourceCreateCloudParam.setCorpKey(corpKey);
        sourceCreateCloudParam.setCode(param.getCode());
        sourceCreateCloudParam.setSource(param.getSource());
        sourceCreateCloudParam.setSourcePersonName(param.getSourcePersonName());
        sourceCreateCloudParam.setSourcePersonPhone(param.getSourcePersonPhone());
        sourceCreateCloudParam.setRemark(param.getRemark());
        sourceCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        sourceCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmSourceCloudService.create(sourceCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessSourceUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getSourcePrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmSourceUpdateCloudParam sourceUpdateCloudParam = new CrmSourceUpdateCloudParam();
        sourceUpdateCloudParam.setSourcePrimaryKey(param.getSourcePrimaryKey());
        sourceUpdateCloudParam.setCode(param.getCode());
        sourceUpdateCloudParam.setSource(param.getSource());
        sourceUpdateCloudParam.setSourcePersonName(param.getSourcePersonName());
        sourceUpdateCloudParam.setSourcePersonPhone(param.getSourcePersonPhone());
        sourceUpdateCloudParam.setRemark(param.getRemark());
        sourceUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        sourceUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmSourceCloudService.update(sourceUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessSourceDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getSourcePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmSourceDeleteCloudParam sourceDeleteCloudParam = new CrmSourceDeleteCloudParam();
        sourceDeleteCloudParam.setSourcePrimaryKeys(param.getSourcePrimaryKeys());
        sourceDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        sourceDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmSourceCloudService.delete(sourceDeleteCloudParam);
    }
}
